<template>
  <div v-show="!showResumeSwitch">
    <el-table :data="checkResume.jobs">
      <el-table-column label="职位名称" prop="name"></el-table-column>
      <el-table-column label="操作" prop="">
        <template #default="scope">
          <el-button @click="openJobInfoDialog(scope.row)">岗位详情</el-button>
          <el-button
              :disabled="checkResume.resume[scope.row.id].length == 0"
              @click="showResume(scope.row)"
          >
            剩余筛选简历数：{{ checkResume.resume[scope.row.id].length }}
          </el-button>
        </template>
      </el-table-column>
    </el-table>
  </div>

  <div v-show="showResumeSwitch">
    <el-page-header @back="goBack">
      <template #content>
        <span class="text-large font-600 mr-3"> 筛选简历 </span>
      </template>
    </el-page-header>
    <el-divider/>
    <el-scrollbar height="400px">
      <el-descriptions :column="2" border title="学生信息">
        <el-descriptions-item label="学号">
          <span>{{ currentStudent.studentId }}</span>
        </el-descriptions-item>
        <el-descriptions-item label="姓名">
          <span>{{ currentStudent.name }}</span>
        </el-descriptions-item>
        <el-descriptions-item label="地址">
          <span>{{ currentStudent.address }}</span>
        </el-descriptions-item>
        <el-descriptions-item label="邮箱">
          <span>{{ currentStudent.email }}</span>
        </el-descriptions-item>
        <el-descriptions-item label="电话">
          <span>{{ currentStudent.phone }}</span>
        </el-descriptions-item>
        <el-descriptions-item label="性别">
          <span>{{ currentStudent.sex == 0 ? '男' : '女' }}</span>
        </el-descriptions-item>
        <el-descriptions-item label="出生日期">
            <span>
                {{ currentStudent.birthDay }}
            </span>
        </el-descriptions-item>
        <el-descriptions-item label="学校">
          <span>{{ currentStudent.school }}</span>
        </el-descriptions-item>
      </el-descriptions>
      <el-divider/>
      <el-descriptions :column="3" border class="des" title="求职信息">
        <el-descriptions-item label="工作年限">
          <span>{{ currentResume.workYear }}</span>
        </el-descriptions-item>
        <el-descriptions-item label="求职意向">
          <span>{{ currentResume.targetJob }}</span>
        </el-descriptions-item>
        <el-descriptions-item label="薪资要求(元)">
          <span>{{ currentResume.targetSalary }}</span>
        </el-descriptions-item>
        <el-descriptions-item :span="3" label="技能特长">
          <span>{{ currentResume.skills }}</span>
        </el-descriptions-item>
        <el-descriptions-item :span="3" label="荣誉证书">
          <span>{{ currentResume.honoraryCertificate }}</span>
        </el-descriptions-item>
        <el-descriptions-item :span="3" label="兴趣爱好">
          <span>{{ currentResume.hobbies }}</span>
        </el-descriptions-item>
        <el-descriptions-item :span="3" label="自我评价">
          <span>{{ currentResume.selfEvaluation }}</span>
        </el-descriptions-item>
      </el-descriptions>
      <el-divider/>
      <div>
        <p class="des-title">教育背景</p>
        <div v-for="ed in currentResume.educationalBackgroundsList" :key="ed.id">
          <el-descriptions :column="2" border class="des">
            <el-descriptions-item label="学校">
              <span>{{ ed.school }}</span>
            </el-descriptions-item>
            <el-descriptions-item label="专业">
              <span>{{ ed.major }}</span>
            </el-descriptions-item>
            <el-descriptions-item label="入学时间">
              <span>{{ ed.startYear.substr(0, 10) }}</span>
            </el-descriptions-item>
            <el-descriptions-item label="毕业时间">
              <span>{{ ed.endYear.substr(0, 10) }}</span>
            </el-descriptions-item>
            <el-descriptions-item label="学历">
              <span>{{ ed.degree }}</span>
            </el-descriptions-item>
            <el-descriptions-item label="详情">
              <div>{{ ed.description }}</div>
            </el-descriptions-item>
          </el-descriptions>
        </div>
      </div>
      <el-divider/>
      <div>
        <p class="des-title">项目经验</p>
        <div v-for="pe in currentResume.projectExperiencesList" :key="pe.id">
          <el-descriptions :column="2" border class="des">
            <el-descriptions-item label="项目名称">
              <span>{{ pe.name }}</span>
            </el-descriptions-item>
            <el-descriptions-item label="担任角色">
              <span>{{ pe.role }}</span>
            </el-descriptions-item>
            <el-descriptions-item label="开始时间">
                    <span>{{
                        pe.startTime instanceof Date ? pe.startTime.toLocaleDateString() : pe.startTime.substr(0, 10)
                      }}</span>
            </el-descriptions-item>
            <el-descriptions-item label="结束时间">
                    <span>{{
                        pe.endTime instanceof Date ? pe.endTime.toLocaleDateString() : pe.endTime.substr(0, 10)
                      }}</span>
            </el-descriptions-item>
            <el-descriptions-item label="详情">
              <div>{{ pe.description }}</div>
            </el-descriptions-item>
          </el-descriptions>
        </div>
      </div>
      <el-divider/>
      <div>
        <p class="des-title">校园经历</p>
        <div v-for="ce in currentResume.campusExperiencesList" :key="ce.id">
          <el-descriptions :column="2" border class="des">
            <el-descriptions-item label="名称">
              <span>{{ ce.name }}</span>
            </el-descriptions-item>
            <el-descriptions-item label="担任角色">
              <span>{{ ce.role }}</span>
            </el-descriptions-item>
            <el-descriptions-item label="开始时间">
                    <span>{{
                        ce.startTime instanceof Date ? ce.startTime.toLocaleDateString() : ce.startTime.substr(0, 10)
                      }}</span>
            </el-descriptions-item>
            <el-descriptions-item label="结束时间">
                    <span>{{
                        ce.endTime instanceof Date ? ce.endTime.toLocaleDateString() : ce.endTime.substr(0, 10)
                      }}</span>
            </el-descriptions-item>
            <el-descriptions-item label="详情">
              <div>{{ ce.description }}</div>
            </el-descriptions-item>
          </el-descriptions>
        </div>
      </div>
      <el-divider/>
      <div>
        <p class="des-title">实习经历</p>
        <div v-for="ie in currentResume.internshipExperiencesList" :key="ie.id">
          <el-descriptions :column="2" border class="des">
            <el-descriptions-item label="公司名称">
              <span>{{ ie.enterpriseName }}</span>
            </el-descriptions-item>
            <el-descriptions-item label="岗位">
              <span>{{ ie.position }}</span>
            </el-descriptions-item>
            <el-descriptions-item label="开始时间">
                    <span>{{
                        ie.startTime instanceof Date ? ie.startTime.toLocaleDateString() : ie.startTime.substr(0, 10)
                      }}</span>
            </el-descriptions-item>
            <el-descriptions-item label="结束时间">
                    <span>{{
                        ie.endTime instanceof Date ? ie.endTime.toLocaleDateString() : ie.endTime.substr(0, 10)
                      }}</span>
            </el-descriptions-item>
            <el-descriptions-item label="详情">
              <div>{{ ie.description }}</div>
            </el-descriptions-item>
          </el-descriptions>
        </div>
      </div>
      <el-divider/>
    </el-scrollbar>
    <div class="operation-button">
      <el-button type="success" @click="openInterviewDialog">通过并发送面试邀请</el-button>
      <el-button type="danger" @click="noPassAndNext(currentResume)">拒绝并下一个</el-button>
    </div>
  </div>
  <div>
    <el-dialog v-model="jobInfoDialog">
      <el-descriptions :column="2" border title="岗位详情">
        <el-descriptions-item label="岗位名称">{{ jobInfo.name }}</el-descriptions-item>
        <el-descriptions-item label="企业名称">{{ jobInfo.enterpriseName }}</el-descriptions-item>
        <el-descriptions-item label="创建日期">
          {{ jobInfo.createdDate.substring(0, 10) }}
        </el-descriptions-item>
        <el-descriptions-item label="更新日期">
          {{ jobInfo.updatedDate }}
        </el-descriptions-item>
        <el-descriptions-item label="职位类型">{{ jobInfo.jobTypeStr }}</el-descriptions-item>
        <el-descriptions-item label="教育背景要求">{{
            jobInfo.educationalRequirementsStr
          }}
        </el-descriptions-item>
        <el-descriptions-item label="工作地址">{{ jobInfo.workAddress }}</el-descriptions-item>
        <el-descriptions-item label="面议工资">{{
            jobInfo.faceTalkSalary ? '是' : '否'
          }}
        </el-descriptions-item>
        <el-descriptions-item v-if="!jobInfo.faceTalkSalary" label="最低薪资">
          {{ jobInfo.minSalary }}{{ jobInfo.minSalaryUnit }}/{{ jobInfo.salaryTime }}
        </el-descriptions-item>
        <el-descriptions-item v-if="!jobInfo.faceTalkSalary" label="最高薪资">
          {{ jobInfo.maxSalary }}{{ jobInfo.maxSalaryUnit }}/{{ jobInfo.salaryTime }}
        </el-descriptions-item>
        <el-descriptions-item label="优先专业" span="2">
          {{ jobInfo.preferredMajors }}
        </el-descriptions-item>
        <el-descriptions-item label="任职要求" span="2">{{ jobInfo.requirements }}</el-descriptions-item>
        <el-descriptions-item label="职位描述" span="2">{{ jobInfo.description }}</el-descriptions-item>
      </el-descriptions>
    </el-dialog>
    <el-dialog v-model="interviewDialog" title="面试邀请">
      <el-form :model="interviewInfo" label-width="100px">
        <!-- 企业名称 -->
        <el-form-item label="企业名称">
          <el-input v-model="interviewInfo.enterpriseName" disabled></el-input>
        </el-form-item>
        <!-- 职位名称 -->
        <el-form-item label="职位名称">
          <el-input v-model="interviewInfo.jobName" disabled></el-input>
        </el-form-item>
        <!-- 学生姓名 -->
        <el-form-item label="学生姓名">
          <el-input v-model="interviewInfo.studentName" disabled></el-input>
        </el-form-item>
        <el-form-item label="学生邮箱">
          <el-input v-model="interviewInfo.studentEmail" disabled></el-input>
        </el-form-item>
        <!-- 面试时间 -->
        <el-form-item label="面试时间">
          <el-date-picker
              v-model="interviewInfo.interviewTime"
              placeholder="选择面试时间"
              type="datetime"
          ></el-date-picker>
        </el-form-item>

        <!-- 地址 -->
        <el-form-item label="地址">
          <el-input v-model="interviewInfo.address"></el-input>
        </el-form-item>

        <!-- 企业联系人 -->
        <el-form-item label="企业联系人">
          <el-input v-model="interviewInfo.contact"></el-input>
        </el-form-item>

        <!-- 联系电话 -->
        <el-form-item label="联系电话">
          <el-input v-model="interviewInfo.phone"></el-input>
        </el-form-item>

        <!-- 表单操作区域 -->
        <el-form-item>
          <el-button v-loading="buttonLoading" :disabled="buttonLoading" type="primary" @click="passResume">发送
          </el-button>
          <el-button @click="interviewDialog = false">取消</el-button>
        </el-form-item>
      </el-form>
    </el-dialog>
  </div>
</template>

<script lang="ts" setup>
import {onMounted, reactive, ref} from "vue";
import type {
  CheckResumeVOInterface,
  EnterpriseInterface,
  JobInterface,
  PassAndInterviewNotificationDTO,
  ResponseInterface,
  ResumeVOInterface,
  StudentInterface
} from "@/model/type";
import {getCheckResumeVOAPI, noPassResumeAPI, passResumeAPI} from "@/api/ResumeAPI";
import {getStudentInfoByIdAPI} from "@/api/UserAPI";
import {ElMessage} from "element-plus";

const enterprise = reactive<EnterpriseInterface>(JSON.parse(localStorage.getItem('user') || '{}'))
const tokenStr = localStorage.getItem('token') || ""
let checkResume = reactive<CheckResumeVOInterface>({});
const jobInfoDialog = ref(false)
const jobInfo = reactive<JobInterface>({})


function openJobInfoDialog(job: JobInterface) {
  jobInfoDialog.value = true
  Object.assign(jobInfo, job)
}

// --- 筛选简历
const currentJob = ref<JobInterface>({})
const showResumeSwitch = ref(false)
const resumeList = ref<Array<ResumeVOInterface>>([])
let resumeListIndex = ref(0)
let currentStudent = reactive<StudentInterface>({})
const currentResume = ref<ResumeVOInterface>({})

async function getCurrentStudent(sId: string) {
  const r: ResponseInterface = await getStudentInfoByIdAPI(sId, tokenStr)
  if (r.code == 200) {
    Object.assign(currentStudent, r.data)
    Object.assign(currentStudent, {birthDay: currentStudent.birthDay.substr(0, 10)})
  }
}

async function showResume(job: JobInterface) {
  currentJob.value = job
  resumeList.value = checkResume.resume[job.id]
  resumeListIndex.value = 0
  currentResume.value = resumeList.value[resumeListIndex.value]
  await getCurrentStudent(currentResume.value.studentId, tokenStr)
  showResumeSwitch.value = true
}

async function goBack() {
  showResumeSwitch.value = false
  const r: ResponseInterface = await getCheckResumeVOAPI(tokenStr)
  if (r.code == 200) {
    Object.assign(checkResume, r.data)
  }
}

function nextResume() {
  if (resumeListIndex.value + 1 >= resumeList.value.length) {
    ElMessage.info("已经是最后一份简历，为您自动返回")
    goBack()
  } else {
    resumeListIndex.value++;
    currentResume.value = resumeList.value[resumeListIndex.value]
    getCurrentStudent(currentResume.value.studentId, tokenStr)
  }
}

async function noPassAndNext(currentResume: ResumeVOInterface) {
  const r: ResponseInterface = await noPassResumeAPI({
    resumeId: currentResume.id,
    jobId: currentJob.value.id,
  }, tokenStr)
  if (r.code == 200) {
    ElMessage.success("操作成功")
    nextResume();
  }
}

// --- 发送面试邀请
let interviewDialog = ref(false)
const buttonLoading = ref(false)
let interviewInfo = reactive<PassAndInterviewNotificationDTO>({
  remark: "", resumeId: "",
  address: "",
  contact: "",
  enterpriseId: "",
  enterpriseName: "",
  interviewTime: undefined,
  jobId: "",
  jobName: "",
  phone: "",
  studentId: "",
  studentName: "",
  studentEmail: ""
})

function openInterviewDialog() {
  interviewDialog.value = true
  initInterviewInfo(interviewInfo)
}

function initInterviewInfo(interviewInfo: PassAndInterviewNotificationDTO) {
  Object.assign(interviewInfo, {
    enterpriseId: enterprise.id,
    enterpriseName: enterprise.name,
    resumeId: currentResume.value.id,
    jobId: currentJob.value.id,
    jobName: currentJob.value.name,
    studentId: currentStudent.id,
    studentName: currentStudent.name,
    studentEmail: currentStudent.email,
    contact: enterprise.contactPerson,
    phone: enterprise.phone,
    address: enterprise.address,
    remark: "",
    interviewTime: undefined
  })
}

async function passResume() {
  if (interviewInfo.interviewTime == undefined) {
    ElMessage.warning("请选择面试时间")
    return;
  }
  buttonLoading.value = true
  const r: ResponseInterface = await passResumeAPI(interviewInfo, tokenStr)
  if (r.code == 200) {
    nextResume();
    interviewDialog.value = false
    buttonLoading.value = false
    ElMessage.success("操作成功，已切换到下一份简历")
  }
}

// ---挂载
onMounted(async () => {
  const r: ResponseInterface = await getCheckResumeVOAPI(tokenStr)
  if (r.code == 200) {
    Object.assign(checkResume, r.data)
  }
})
</script>

<style scoped>
.des {
  margin-top: 20px;
}

.des-title {
  font-size: 16px;
  font-weight: 700;
}

.operation-button {
  margin-top: 20px;
}
</style>